<template>
  <header class="header">
    <div class="header-container">
      <div class="logo">
        <router-link to="/">
          <img src="../assets/cnblogs-logo.svg" alt="博客园" />
        </router-link>
      </div>
      <nav class="main-nav">
        <ul>
          <li><router-link to="/">首页</router-link></li>
        </ul>
      </nav>
      <div class="user-actions">
        <template v-if="!userStore.isLogin">
          <router-link to="/login" class="login-btn">登录</router-link>
          <router-link to="/register" class="register-btn">注册</router-link>
        </template>
        <template v-else>
          <div class="user-section">
            <router-link 
              :to="{ name: 'userProfile', params: { id: userStore.currentUser.id }}" 
              class="user-info"
            >
              <img 
                :src="userStore.currentUser.avatar" 
                :alt="userStore.currentUser.username" 
                class="user-avatar"
              >
              <span class="username">{{ userStore.currentUser.username }}</span>
            </router-link>
            <button @click="handleLogout" class="logout-btn">
              <i class="fas fa-sign-out-alt"></i>
              退出
            </button>
          </div>
        </template>
      </div>
    </div>
  </header>
</template>

<script setup>
import { useMenuStore } from '../stores/useMenuStore'
import { useUserStore } from '../stores/userStore'
import { useRouter } from 'vue-router'

const menuStore = useMenuStore()
const userStore = useUserStore()
const router = useRouter()

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.header {
  background: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  flex-shrink: 0;
}

.logo a {
  display: block;
  text-decoration: none;
}

.logo img {
  height: 35px;
  width: auto;
}

.main-nav {
  margin-right: auto;
  margin-left: 32px;
}

.main-nav ul {
  display: flex;
  gap: 24px;
  list-style: none;
  padding: 0;
  margin: 0;
}

.main-nav a {
  color: var(--text-color);
  text-decoration: none;
  font-size: 15px;
  padding: 8px 0;
}

.main-nav a:hover,
.main-nav a.router-link-active {
  color: var(--primary-color);
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: inherit;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.user-info:hover {
  background-color: var(--hover-color);
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.username {
  font-size: 14px;
  color: var(--text-color);
}

.logout-btn {
  background: none;
  border: none;
  color: var(--text-light);
  cursor: pointer;
  font-size: 14px;
  padding: 4px 8px;
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 4px;
  transition: all 0.2s;
}

.logout-btn:hover {
  color: var(--primary-color);
  background-color: var(--hover-color);
}

.logout-btn i {
  font-size: 12px;
}

.login-btn,
.register-btn {
  padding: 6px 16px;
  border-radius: 4px;
  text-decoration: none;
  font-size: 14px;
}

.login-btn {
  color: var(--primary-color);
  border: 1px solid var(--primary-color);
}

.register-btn {
  background: var(--primary-color);
  color: white;
  border: 1px solid var(--primary-color);
}

.login-btn:hover {
  background: rgba(0, 102, 204, 0.1);
}

.register-btn:hover {
  background: #0052a3;
}

.user-section {
  display: flex;
  align-items: center;
  gap: 8px;
}
</style>